<?php
/*
Model: 用户
*/

class User_model extends CI_Model
{
	function __construct()
	{
			parent::__construct();
	}

	//新用户注册
	function register()
	{
		//新增未激活用户
		$array = array(
			'account' => $this->input->post('account'),
			'password' => md5($this->input->post('password')),
			'name' => $this->input->post('name'),
			'user_gender' => $this->input->post('user_gender'),
			'company_name' => $this->input->post('company_name'),
			'mobile' => $this->input->post('mobile'),
			'address' => $this->input->post('useraddress'),
			'postcode' => $this->input->post('userpostcode'),
			'account_type' => $this->input->post('account_type'),
			);
		$this->db->set($array);
		$this->db->insert('iqs_user');
	}

	//发送注册成功邮件
	function send_register_mail()
	{
		//生成验证随机数
		$valid_code = $this->myfunctions->getCode(16);
		
		//客户电子邮件
		$email = $this->input->post('account');

		//邮件内容
		$msg = _("欢迎注册").SITE_NAME."\n-----------------------\n";
		$msg .= _("请并访问以下链接，输入以激活您的账户:")."\n";
		$msg .= site_url("/user/activation/")."\n";
		$msg .= _("验证随机码").":".$valid_code;

		//新增激活请求记录
		$insert = array(
			 'uid' => $this->db->insert_id(),
			 'account' => $this->input->post('account'),
			 'valid_code' => $valid_code,
		);
		$this->db->set($insert);
		$q_id = $this->db->insert('iqs_validaccount');
		
		//发送账户激活邮件
		$this->myfunctions->sendmail(SMTP_EMAIL,SITE_NAME,$email,_("欢迎注册").SITE_NAME.","._("请激活您的账户"),$msg);
	}

	//用户登录
	function login($account,$password)
	{
		//检测系统是否已经存在该用户
		$q = $this->db->query("select * from iqs_user where (account='".$account."' OR mobile='".$account."') AND password=md5('".$password."') AND account_status = '2'");
		$row = $q->result();
		return (!$row) ? false : true;
	}

	//查询用户资料
	function get_user_info($where)
	{
		$q = $this->db->query("select * from iqs_user ".$where);
		$row = $q->result();
		return !$row ? false : $row[0];
	}

	//检测重复用户
	function check_same_user($uid)
	{
		$q = $this->db->query("select count(*) as num from iqs_user where id='".$uid."'");
		$row = $q->result();
		return (!$row[0]->num || $row[0]->num==0) ? false : true;
	}

	//检测账户激活状态
	function check_user_is_activation($valid_code)
	{
		$q = $this->db->query("select a.* from iqs_user a,iqs_validaccount b where a.id=b.uid and b.status='1' and b.valid_code='".$valid_code."'");
		return (!$q->result()) ? false : $q->result();
	}

	//更新帐户基本信息
	function update_account_baseinfo()
	{
		$update = array(
			 'address' => $this->input->post('address'),
			 'postcode' => $this->input->post('postcode'),
		);

		if( $this->input->post('company_name') ) { $update['company_name'] = $this->input->post('company_name'); }

		$this->db->where('id', $this->session->userdata('uid'));
		$this->db->update('iqs_user', $update);
	}

	//重新设定帐户密码
	function update_account_password()
	{
		$update = array(
			 'password' => md5($this->input->post('new_password')),
		);

		$this->db->where('id', $this->session->userdata('uid'));
		$this->db->update('iqs_user', $update);
	}

	//重新设定帐户密码(忘记密码)
	function setup_new_password()
	{
		$check_valid_password = $this->check_valid_password();

		//重新设定帐户密码
		if($check_valid_password)
		{
			//更新密码
			$update = array(
				 'password' => md5($this->input->post('new_password')),
			);

			$this->db->where('account', $check_valid_password->email);
			$this->db->update('iqs_user', $update);

			//更新重设密码记录
			$update2 = array(
				 'update_password' => $this->input->post('new_password'),
				 'upd_time' => date("Y-m-d H:i:s"),
				 'status' => 2,
			);

			$this->db->where('id', $check_valid_password->id);
			$this->db->update('iqs_validpassword', $update2);

			return true;
		}
		else
		{
			return false;
		}
	}

	//发送帐户/电子邮件更新请求邮件
	function send_valid_email()
	{
		//生成验证随机数
		$valid_code = $this->myfunctions->getCode();

		//邮件内容
		$msg = _("您的帐户/电子邮件更新")."\n-----------------------\n";
		$msg .= _("请登录后访问以下链接并输入验证随机码")."\n";
		$msg .= site_url("user/editemail/")."\n";
		$msg .= _("验证随机码").":".$valid_code;
		
		//发送更新请求邮件
		$this->myfunctions->sendmail(SMTP_EMAIL,SITE_NAME,$this->input->post('valid_email'),SITE_NAME._("帐户/电子邮件更新"),$msg);

		//新增请求记录
		$insert = array(
			 'uid' => $this->session->userdata('uid'),
			 'update_email' => $this->input->post('valid_email'),
			 'valid_code' => $valid_code,
			 'q_time' => date("Y-m-d H:i:s"),
		);
		$this->db->set($insert);
		$this->db->insert('iqs_validemail');
	}

	//发送手机号码更新请求短信
	function send_valid_mobile()
	{
		//生成验证随机数
		$valid_code = $this->myfunctions->getCode();

		//短信内容
		$msg = "您的".SITE_NAME._("验证随机码").": \n".$valid_code;

		//新增请求记录
		$insert = array(
			 'uid' => $this->session->userdata('uid'),
			 'update_mobile' => $this->input->post('valid_mobile'),
			 'valid_code' => $valid_code,
			 'q_time' => date("Y-m-d H:i:s"),
		);
		$this->db->set($insert);
		$q_id = $this->db->insert('iqs_validmobile');
		
		//发送更新请求短信
		$this->myfunctions->sendmsg( $this->input->post('valid_mobile'), $msg, $this->myfunctions->add_zero($q_id,1,6,0) );
	}

	//发送忘记密码验证邮件
	function send_valid_password()
	{
		//生成验证随机数
		$valid_code = $this->myfunctions->getCode(16);

		//邮件内容
		$msg = _("找回您在").SITE_NAME._("网站的密码")."\n-----------------------\n";
		$msg .= _("根据您在").SITE_NAME._("网站的“忘记密码”请求，我们需要您在收到此邮件后登录").SITE_NAME._("网站并访问以下链接，输入验证随机码")."\n";
		$msg .= site_url("user/setup_new_password/")."\n";
		$msg .= _("验证随机码:").$valid_code;
		
		//发送忘记密码验证邮件
		$this->myfunctions->sendmail(SMTP_EMAIL,SITE_NAME,$this->input->post('valid_email'),_("找回您在").SITE_NAME._("网站的密码"),$msg);

		//新增请求记录
		$insert = array(
			 'email' => $this->input->post('valid_email'),
			 'valid_code' => $valid_code,
			 'q_time' => date("Y-m-d H:i:s"),
		);
		$this->db->set($insert);
		$this->db->insert('iqs_validpassword');
	}

	//检测当前是否存在帐户激活请求
	function check_valid_account($valid_code)
	{
		$q = $this->db->query("select * from iqs_validaccount where valid_code='".$valid_code."' and status=1 order by q_time desc limit 1");
		$row = $q->result();
		return !$row ? false : $row[0];
	}

	//检测当前是否存在帐户/电子邮件更新请求
	function check_valid_email()
	{
		$q = $this->db->query("select * from iqs_validemail where uid='".$this->session->userdata('uid')."' and status=1 order by q_time desc limit 1");
		$row = $q->result();
		return !$row ? false : $row[0];
	}

	//检测当前是否存在手机号码更新请求
	function check_valid_mobile()
	{
		$q = $this->db->query("select * from iqs_validmobile where uid='".$this->session->userdata('uid')."' and status='1' order by q_time desc limit 1");
		$row = $q->result();
		return !$row ? false : $row[0];
	}

	//检测当前帐户/电子邮件更新验证码
	function check_valid_email_code($valid_code)
	{
		$q = $this->db->query("select * from iqs_validemail where valid_code='".$valid_code."' and uid='".$this->session->userdata('uid')."' and status=1 order by q_time desc limit 1");
		$row = $q->result();
		return !$row ? false : $row[0];
	}

	//检测当前手机号码更新验证码
	function check_valid_mobile_code($valid_code)
	{
		$q = $this->db->query("select * from iqs_validmobile where valid_code='".$valid_code."' and uid='".$this->session->userdata('uid')."' and status='1' order by q_time desc limit 1");
		$row = $q->result();
		return !$row ? false : $row[0];
	}

	//检测当前是否存在重设密码请求
	function check_valid_password()
	{
		$q = $this->db->query("select * from iqs_validpassword where valid_code='".$this->input->post('valid_code')."' and status=1 order by q_time desc limit 1 ");
		$row = $q->result();
		return !$row ? false : $row[0];
	}

	//更新帐户/电子邮件
	function do_activation()
	{
		//查询要激活的账户
		$valid_code = trim($this->input->post("valid_code"));
		$activation = $this->check_valid_account($valid_code);

		//激活账户
		if(!$activation)
		{
			return false;
		}
		else
		{
			//激活帐户
			$update = array(
				 'account_status' => "2",
			);
			$this->db->where('id', $activation->uid);
			$this->db->update('iqs_user', $update);

			//更新验证记录
			$update2 = array(
				 'upd_time' => date("Y-m-d H:i:s"),
				 'status' => 2,
			);
			$this->db->where('id', $activation->id);
			$this->db->update('iqs_validaccount', $update2);

			return true;
		}
	}

	//更新帐户/电子邮件
	function do_valid_email()
	{
		//查询要更新的电子邮件
		$update_email = $this->check_valid_email();
		
		//更新帐户/电子邮件
		if(!$update_email->update_email)
		{
			return false;
		}
		else
		{
			//更新帐户/电子邮件
			$update = array(
				 'account' => $update_email->update_email,
			);
			$this->db->where('id', $this->session->userdata('uid'));
			$this->db->update('iqs_user', $update);

			//更新验证记录
			$update2 = array(
				 'upd_time' => date("Y-m-d H:i:s"),
				 'status' => 2,
			);
			$this->db->where('id', $update_email->id);
			$this->db->update('iqs_validemail', $update2);

			return true;
		}
	}

	//更新手机号码
	function do_valid_mobile()
	{
		//查询要更新的手机号码
		$update_mobile = $this->check_valid_mobile();

		//更新手机号
		if(!$update_mobile->update_mobile)
		{
			return false;
		}
		else
		{
			//更新手机号
			$update = array(
				 'mobile' => $update_mobile->update_mobile,
			);
			$this->db->where('id', $this->session->userdata('uid'));
			$this->db->update('iqs_user', $update);

			//更新验证记录
			$update2 = array(
				 'upd_time' => date("Y-m-d H:i:s"),
				 'status' => 2,
			);
			$this->db->where('id', $update_mobile->id);
			$this->db->update('iqs_validmobile', $update2);

			return true;
		}
	}
}
?>